package com.apes.scm.masterdata.enumeration.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.model.EnumerationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;


/**
 *
 * @author xul
 * @create 2018-02-06 16:47
 */
public interface EnumerationRepository extends CustomRepository<Enumeration, String> {
    @Query("select T from Enumeration T where T.id =:id")
    Enumeration findEnumeration(@Param("id") String id);

    Enumeration findByDescriptionAndTypeId(@Param("value") String value, @Param("type") String type);

    Page<Enumeration> findByType(EnumerationType enumerationType, Pageable pageable);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :value  order by T.id")
    List<Enumeration> findByTypeOrderByDescription(@Param("value") String value);

    Page<Enumeration> findByTypeAndValidIsTrue(EnumerationType enumerationType, Pageable pageable);

    @Query("select T from Enumeration T where ENUMERATION_TYPE_ID = :type  and (id like %:value%  or description like %:value%) ")
    Page<Enumeration> findByTypeAndKey(@Param("value") String value,@Param("type") String type, Pageable pageable);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :value ")
    List<Enumeration> findByType(@Param("value") String value);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :type and (id like %:value%  or description like %:value%)")
    Enumeration findByTypeAndValue(@Param("value") String value,@Param("type") String type);

    @Query("SELECT T FROM Enumeration T WHERE T.type.id = :value and T.valid=true ")
    List<Enumeration> findByTypeAndValid(@Param("value") String value);

    Enumeration findByCodeAndValidIsTrue(String code);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :type and code = :code")
    Enumeration findByTypeAndCode(@Param("type") String type, @Param("code") String code);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :type and description = :description")
    Enumeration findByTypeAndDescription(@Param("type") String type, @Param("description") String description);

    @Query("SELECT T FROM Enumeration T WHERE T.type.id = :value and T.valid = true AND ID NOT IN (:id) ORDER BY CODE DESC ")
    List<Enumeration> findDepotLevels(@Param("value") String value, @Param("id") List id);

    @Query("select T from Enumeration T where T.id in (:id) order by T.code ")
    List<Enumeration> findEnumerationId(List id);

    @Query("SELECT T FROM Enumeration T WHERE ENUMERATION_TYPE_ID = :value ORDER BY T.createDate")
    List<Enumeration> findByTypeAndDate(@Param("value") String value);

    @Query("SELECT A.id as id ,A.description as description FROM Enumeration A WHERE A.id = :id")
    Map<String, Object> findIdAndName(@Param("id") String fnTag);
}
